package com.zuoqi.easywork.system.controller;

import com.zuoqi.common.utils.IMap;
import com.zuoqi.common.utils.ReturnMapUtils;
import com.zuoqi.common.utils.SessionUtil;
import com.zuoqi.easywork.userManage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class userController {

    @Autowired
    private UserService userService;

    @RequestMapping("/userCon/toPage/changePwd")
    public String changePwdViwe(){
        return "/module/system/changePwd";
    }

    @RequestMapping(value = "/userCon/ajax/changePwd",method = RequestMethod.POST)
    @ResponseBody
    public Map changePwd (HttpServletRequest request){
        IMap param = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        String oldPassword = param.getString( "old_password", "");
        String password = param.getString("password", "");
        String confirmPassword = param.getString( "confirm_password", "");
        if (!password.equals(confirmPassword)) {
            // 两次密码不一致
            return ReturnMapUtils.businessResult("9999","两次密码输入不一致");
        }

        int i = userService.updateUserPwd(userId,oldPassword, password);
        if(i==1){
            return ReturnMapUtils.businessResult("0000","修改密码成功");
        }else{
            return ReturnMapUtils.businessResult("9999","旧密码错误");
        }
    }

    @RequestMapping(value = "/userCon/ajax/getUserDp",method = RequestMethod.POST)
    @ResponseBody
    public Map getUserDp(HttpServletRequest request){
        IMap param = new IMap(request);
        IMap departName = userService.getUserDp(param.getString("userId"));
        return ReturnMapUtils.businessResult("0000","",departName);
    }
}
